package com.snap.vseries.dialog;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;

import com.aigestudio.wheelpicker.WheelPicker;
import com.snap.vseries.R;
import com.snap.vseries.ui.widget.TextButton;

import java.util.ArrayList;
import java.util.List;

public class TempDurationPickerDialog extends DialogFragment implements View.OnClickListener, WheelPicker.OnItemSelectedListener {

    TextButton confirmBtn;
    TextButton cancelBtn;

    WheelPicker tempLeftPicker;
    WheelPicker tempRightPicker;
    WheelPicker durationLeftPicker;
    WheelPicker durationRightPicker;

    private float currentTemp;
    private int currentDuration;
    private OnTempDurationSelectedListener listener;

    public DialogFragment setParam(float currentTemp, int currentDuration, OnTempDurationSelectedListener listener) {
        if (currentTemp < 10.0f) {
            currentTemp = 10.0f;
        } else if (currentTemp > 111.9f) {
            currentTemp = 111.9f;
        }
        if (currentDuration < 0) {
            currentDuration = 0;
        } else if (currentDuration > 3599) {
            currentDuration = 3599;
        }
        this.currentTemp = currentTemp;
        this.currentDuration = currentDuration;
        this.listener = listener;
        return this;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(STYLE_NO_TITLE, R.style.DimDialogStyle);
    }

    @Override
    public void onStart() {
        super.onStart();
        WindowManager.LayoutParams layoutParams = getDialog().getWindow().getAttributes();
        layoutParams.gravity = Gravity.BOTTOM;
        layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
        getDialog().getWindow().setAttributes(layoutParams);
        getDialog().setCanceledOnTouchOutside(true);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.dialog_temp_duration_picker, container, false);
        confirmBtn = v.findViewById(R.id.confirm_btn);
        cancelBtn = v.findViewById(R.id.cancel_btn);
        confirmBtn.setOnClickListener(this);
        cancelBtn.setOnClickListener(this);

        tempLeftPicker = v.findViewById(R.id.temp_left_picker);
        tempRightPicker = v.findViewById(R.id.temp_right_picker);
        durationLeftPicker = v.findViewById(R.id.duration_left_picker);
        durationRightPicker = v.findViewById(R.id.duration_right_picker);
        WheelPicker[] pickers = {tempLeftPicker, tempRightPicker, durationLeftPicker, durationRightPicker};
        for (WheelPicker picker : pickers) {
            picker.setSelectedItemTextColor(Color.BLACK);
            picker.setItemTextColor(Color.parseColor("#60000000"));
            picker.setIndicator(true);
            picker.setIndicatorColor(getContext().getResources().getColor(R.color.color_divider));
            picker.setIndicatorSize(2);
            picker.setVisibleItemCount(5);
            picker.setCyclic(true);
            picker.setCurved(false);
        }

        //温度左
        List<Integer> tempLeftList = new ArrayList<>();
        for (int i = 10; i < 120; i++) {
            tempLeftList.add(i);
        }
        tempLeftPicker.setData(tempLeftList);
        int tempInt = (int) currentTemp;
        tempLeftPicker.setSelectedItemPosition(tempInt - 10);
        //温度右
        List<Integer> tempRightList = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            tempRightList.add(i);
        }
        tempRightPicker.setData(tempRightList);
        int tempDecimal = Math.round((currentTemp - tempInt) * 10f);
        tempRightPicker.setSelectedItemPosition(tempDecimal);
        //时间-分钟
        List<Integer> durationLeftList = new ArrayList<>();
        for (int i = 0; i < 60; i++) {
            durationLeftList.add(i);
        }
        durationLeftPicker.setData(durationLeftList);
        int durationMinute = currentDuration / 60;
        durationLeftPicker.setSelectedItemPosition(durationMinute);
        //时间-秒钟
        List<Integer> durationRightList = new ArrayList<>();
        for (int i = 0; i < 60; i++) {
            durationRightList.add(i);
        }
        durationRightPicker.setData(durationRightList);
        int durationSec = currentDuration % 60;
        durationRightPicker.setSelectedItemPosition(durationSec);

        tempLeftPicker.setOnItemSelectedListener(this);
        tempRightPicker.setOnItemSelectedListener(this);
        durationLeftPicker.setOnItemSelectedListener(this);
        durationRightPicker.setOnItemSelectedListener(this);
        return v;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.confirm_btn:
                if (listener != null) {
                    listener.onTempDurationSelected(currentTemp, currentDuration);
                }
            case R.id.cancel_btn:
                dismiss();
                break;
        }
    }

    @Override
    public void onItemSelected(WheelPicker picker, Object data, int position) {
        int tempInt = tempLeftPicker.getCurrentItemPosition() + 10;
        int tempDecimal = tempRightPicker.getCurrentItemPosition();
        currentTemp = tempInt + tempDecimal / 10f;
        int durationMinute = durationLeftPicker.getCurrentItemPosition();
        int durationSec = durationRightPicker.getCurrentItemPosition();
        currentDuration = durationMinute * 60 + durationSec;
    }

    public interface OnTempDurationSelectedListener {
        void onTempDurationSelected(float temp, int duration);
    }
}